package com.kdgz.kdgz.thread;

import com.kdgz.kdgz.beans.Orders;
import com.kdgz.kdgz.beans.Staff;
import com.kdgz.kdgz.sevice.OrdersService;
import com.kdgz.kdgz.sevice.StaffService;
import com.kdgz.kdgz.util.SendOrders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.List;
import java.util.Random;

/**
 * @Author Veneno
 * @Desc 模拟员工派送
 * @date 2021/3/20
 */
@Component
@Order(value = 13)
public class StaffThread implements ApplicationRunner {

    @Autowired
    StaffService staffService;
    @Autowired
    OrdersService ordersService;
    @Autowired
    SendOrders sendOrders;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Calendar calendar = Calendar.getInstance();
                while (true) {
                        // 0. 读取所有快递员工
                        List<Staff> list = staffService.getOrderStaff();
                        for (int i = 0; i < list.size(); i++) {
                            Staff staff = list.get(i);
                            // 判断是否是经理,与中心点距离是否为0,是否分配了区域
                            if (staff.getBid() != 999 && staff.getDistance() <= 0 && staff.getAid() != 1 && staff.getStatus() == 0) {
                                // 早上8点启动！晚上8点下班
                                if (calendar.get(Calendar.HOUR_OF_DAY) >= 8 && calendar.get(Calendar.HOUR_OF_DAY) < 20) {
                                    // 1.3 装货
                                    List<Orders> ordersBySid = ordersService.getOrdersBySidAndStatus(staff.getSid(),0);
                                    if(ordersBySid.size()==0){
                                        sendOrders.send(staff);
                                        ordersBySid=ordersService.getOrdersBySidAndStatus(staff.getSid(),0);
                                        if(ordersBySid.size()==0){
                                            continue;
                                        }
                                    }
                                    // 1.1 修改员工状态(0->1)
                                    staff.setStatus(1);
                                    // 1.2 生成随机速度
                                    staff.setSpeed(new Random().nextInt(20)+20);
                                    for (int x=0;x<ordersBySid.size();x++) {
                                        ordersBySid.get(x).setStatus(1); // 派送中
                                        ordersService.updateOrder(ordersBySid.get(x));
                                    }
                                }
                            } else if (staff.getBid() != 999 && staff.getAid() != 1) {
                                if(staff.getStatus()==1){
                                    staff.setDistance(staff.getDistance() + staff.getSpeed());
                                }else if(staff.getStatus()==2){
                                    staff.setDistance(staff.getDistance() - staff.getSpeed());
                                }
                                double x = staff.getArea().getX();
                                double y = staff.getArea().getY();
                                    // 如果状态为1就改为2
                                    if (x * x + y * y <= staff.getDistance() * staff.getDistance()&&staff.getStatus() == 1) {
                                        staff.setStatus(2);
                                        // 卸货
                                        List<Orders> ordersBySid = ordersService.getOrdersBySidAndStatus(staff.getSid(),1);
                                        for (Orders orders : ordersBySid) {
                                            orders.setStatus(2);// 签收
                                            ordersService.updateOrder(orders);
                                        }
                                    } else if (staff.getStatus() == 2 && staff.getDistance()<=0) {
                                        // 如果状态为2就改为0
                                        staff.setStatus(0);
                                    }
                            }
                            // 2. 更新数据库？
                            staffService.updateStaff(staff);
                        }
                        try {
                            //员工线程周期
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
        };
        new Thread(runnable).start();
    }
}
